/**
 * Copyright (c) 2009 blurredVision
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 
 * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include "StdAfx.h"
#include "../guidefaults.h"
#include "../DropDown.h"

namespace gui{
	
DropDown::DropDown(void)
{
	// Create the list element
	m_pList = new List();
	m_pList->setPosition(vPos.x, vPos.y+iRowSize);
	m_pList->setVisible(false);
	// Add this component as mouse listener for the list
	m_pList->addMouseListener(this);
	m_pList->addChangeListener(this);
	// Add the list as a sub component
	this->add(m_pList);
	vDim.y = iRowSize;
}

DropDown::~DropDown(void)
{
}

void DropDown::setTitle(const std::string &str)
{
	m_sTitle = str;
	Component::ExecUpdate();
}

void DropDown::addString(const std::string &str)
{
	m_pList->addString(str);
	Component::ExecUpdate();
}

void DropDown::removeString(const std::string &str)
{
	m_pList->removeString(str);
}

void DropDown::setDimension(int x, int y, int w, int h)
{
	m_pList->setSize(w, m_pList->getSize().y);
	Component::setDimension(x, y, w, iRowSize);
}

void DropDown::setNumberOfVisibleEntries(int val) 
{
	m_pList->setNumberOfVisibleEntries(val);
}

void DropDown::setScrollSteps(int val) {
	m_pList->setScrollSteps(val);
}

void DropDown::draw()
{
	if (bVisible) {
		vRealPos = getAbsolutePosition();
		if (bEnabled) {
			// Draw the background
			pRenderer->drawFilledRect(vRealPos.x , vRealPos.y, vDim.x, vDim.y, vBackColor);
			pRenderer->drawRect(vRealPos.x , vRealPos.y, vDim.x, iRowSize, vBorderColor);
			if (m_pList->getSelectedIndex() != -1)
				font->print(m_pList->getSelectedItem(), vRealPos.x+vOffset.x, vRealPos.y+vOffset.x, 0, FONT_LEFT, 0, 0, vTextColor);
			else 
				font->print(m_sTitle, vRealPos.x+vOffset.x, vRealPos.y+vOffset.x, 0, FONT_LEFT, 0, 0, vTextColor);
		} else {
			// Draw the background
			pRenderer->drawFilledRect(vRealPos.x , vRealPos.y, vDim.x, vDim.y, vBackColor * 0.75f);
			pRenderer->drawRect(vRealPos.x , vRealPos.y, vDim.x, iRowSize, vBorderColor);
			if (m_pList->getSelectedIndex() != -1)
				font->print(m_pList->getSelectedItem(), vRealPos.x+vOffset.x, vRealPos.y+vOffset.x, 0, FONT_LEFT, 0, 0, vTextColor * 0.75f);
			else 
				font->print(m_sTitle, vRealPos.x+vOffset.x, vRealPos.y+vOffset.x, 0, FONT_LEFT, 0, 0, vTextColor * 0.75f);
		}
		drawElements();
	}
}

void DropDown::onMousePressed(const MouseEvent &me)
{
	if (bEnabled) {
		m_pList->setVisible(!m_pList->isVisible());
		bringToFront();
		Component::onMousePressed(me);
	}
}

void DropDown::onMouseLeft(const MouseEvent &me)
{
	Component::onMouseLeft(me);
}

void DropDown::onMouseLeaveEvent(const MouseEvent &mevent)
{	
	if (!bMouseEntered)
		m_pList->setVisible(false);	
}

void DropDown::onMouseReleaseEvent(const MouseEvent &mevent)
{
	m_pList->setVisible(false);
}

void DropDown::onChangeEvent(const ChangeEvent &ce) 
{
	// Just pass change events from the list to the next element
	valueChanged(this);
}

std::string DropDown::getSelectedItem() const
{
	return m_pList->getSelectedItem();
}

int DropDown::getSelectedIndex() const
{
	return m_pList->getSelectedIndex();
}

void DropDown::setSelectedIndex(int id)
{
	m_pList->setSelectedIndex(id);
}

void DropDown::clear()
{
	m_pList->clear();
}

void DropDown::setFont(IFont* font)
{
	Component::setFont(font);
	m_pList->setFont(font);
	iRowSize = font->getSize();
	vDim.y = iRowSize;
	m_pList->setPosition(0, iRowSize);
}

const unsigned int DropDown::getNumItems() const
{
	return m_pList->getNumItems();
}

} // namespace
